#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import logging
import time
import datetime
import os
import oauth
import simplejson
import urllib
import random
from init import *
"""

"""
CONSUMER_KEY = '6LMfpjqrLx0x5IjB2aA7tg'
CONSUMER_SECRET_KEY = 'ErWZIty5P4hxwQU90oDwpNR4MDbMDisZiJxdvQRbmIk'
ACCESS_TOKEN = '229879833-eAkxzHejloE8c4QrS95V2nMvVDEnPwCNeANhApof'
ACCESS_TOKEN_SECRET = 'WOr3q8vRbVjcKQGQ8gDqoggnCfkVyTP6wUiHahHs'


class Tweet(webapp.RequestHandler):

    def get(self):
        #callback_url = "http://%s:8080/" % os.environ["REMOTE_ADDR"]
        client = oauth.TwitterClient(CONSUMER_KEY, CONSUMER_SECRET_KEY, None)
        message=Bweet().Get()
        param = {'status': message}        
        try:
            res = client.make_request('http://api.twitter.com/1/statuses/update.json',
                            token=ACCESS_TOKEN,
                            secret=ACCESS_TOKEN_SECRET,
                            additional_params=param,
                            method='POST')
            if res.status_code != 200:
                logging.warning('Failed to post a message to twitter. status_code: %d' \
                                % res.status_code)
                self.response.out.write('Error Status:' + res.status_code)
                self.response.headers['Content-Type'] = 'application/json'
            self.response.out.write(res.status_code)
        except Exception, e:
            logging.error(e)

application = webapp.WSGIApplication([('/tweet', Tweet)], debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
